﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BOHVote.Common;
using BOHVote.BLL;
using System.Data;
using System.Web.UI.HtmlControls;

namespace BOH.Information.Report
{
    public partial class PrintPersonalReport : System.Web.UI.Page
    {
        private int intRecordCount = 0;
        private int CurrentCount = 1;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.Title = common.getAppSetting("AppName");
                string SubjectName = string.Empty;
                string GovernName = string.Empty;
                string ApplyImg = string.Empty;
                string Period = string.Empty;
                if (Request.QueryString["ID"] != null && !string.IsNullOrEmpty(Request.QueryString["ID"].ToString()))
                {
                    string ApplyID = Request.QueryString["ID"].ToString();

                    //XSD.PersonalInfo xsd = BindReport(ApplyID, out SubjectName, out GovernName, out ApplyImg, out Period);

                    //ReportViewBind(xsd);//, SubjectName, GovernName, ApplyImg, "", Period);
                    DataSet dstApply = FillForm(ApplyID);
                    intRecordCount = dstApply.Tables[0].Rows.Count;
                    BindPersonalList(dstApply);
                }
                else if ( Request.QueryString["ApplyType"] != null && Request.QueryString["Govern"] != null && Request.QueryString["Unit"] != null && Request.QueryString["Subject"] != null && Request.QueryString["Speciality"] != null && Request.QueryString["ApplyJobRank"] != null)
                {

                    string ApplyType = Request.QueryString["ApplyType"].ToString();
                    string Govern = Request.QueryString["Govern"].ToString();
                    string Unit = Request.QueryString["Unit"].ToString();
                    string Subject = Request.QueryString["Subject"].ToString();
                    string Speciality = Request.QueryString["Speciality"].ToString();
                    string ApplyJobRank = Request.QueryString["ApplyJobRank"].ToString();
                    DataSet dst = dstPersonalList(Unit, Govern, string.Empty, ApplyType, Speciality, Subject, ApplyJobRank,string.Empty, (string)Session["ApplyPeriod"]);

                    //intRecordCount = dst.Tables[0].Rows.Count;
                    BindPersonalList(dst);
                }
            }
        }

        public DataSet  FillForm(string ID)
        {
            DataSet dstApply = ApplyClass.getApplyInfoByApplyID(ID);
            return dstApply;
        }

        public DataSet dstPersonalList(string Unit, string Govern, string ApplyName, string ApplyType, string Speciality, string Subject, string ApplyJobRank,string IDCard, string ApplyPeriod)
        {
            DataSet dst = ApplyClass.dstAllInfoApplys(Unit, Govern, ApplyName, ApplyType, Speciality, Subject, ApplyJobRank,IDCard, (string)Session["ApplyPeriod"]);
            return dst;
        }

        public void BindPersonalList(DataSet dst)
        {
            XSD.PersonalInfo personalXSD = new BOH.XSD.PersonalInfo();
            string ApplyImage = string.Empty;
            string strPeriod = string.Empty;
            intRecordCount = dst.Tables[0].Rows.Count;
            foreach (DataRow dr in dst.Tables[0].Rows)
            {
                DataRow drNew = personalXSD.Tables["PersonalInfo"].NewRow();
                drNew["ApplyID"] = dr["ApplyID"].ToString();
                drNew["ApplyName"] = dr["Apply_Name"].ToString();
                drNew["ApplyMobile"] = dr["ApplyMobile"].ToString();
                drNew["UnitGovernName"] = dr["UnitGovernName"].ToString();
                drNew["GovernName"] = dr["GovernName"].ToString();
                if (!String.IsNullOrEmpty(dr["ApplyPeriodID"].ToString()))
                    drNew["ApplyPeriod"] = dr["ApplyPeriodID"].ToString() + "年度";
                else
                    drNew["ApplyPeriod"] = string.Empty;
                drNew["Gender"] = dr["Gender"].ToString();

                if (!string.IsNullOrEmpty(dr["Birthday"].ToString()))
                    drNew["Birthday"] = Convert.ToDateTime(dr["Birthday"].ToString()).ToString("yyyy-MM");
                else
                    drNew["Birthday"] = "-";

                drNew["EducationLevel"] = dr["EducationLevel"].ToString();
                drNew["IDCard"] = dr["IDCard"].ToString();
                drNew["School"] = dr["School"].ToString();
                if (!string.IsNullOrEmpty(dr["GraduationDate"].ToString()))
                    drNew["GraduationDate"] = Convert.ToDateTime(dr["GraduationDate"].ToString()).ToString("yyyy-MM");
                else
                    drNew["GraduationDate"] = "-";

                drNew["CurrentJobRank"] = dr["CurrentJobRankID"].ToString();
                drNew["ApplyType"] = dr["ApplyType"].ToString();
                drNew["SpecialityID"] = dr["SpecialityID"].ToString();
                drNew["JobRankID"] = dr["JobRankID"].ToString();
                drNew["SubjectID"] = dr["SubjectID"].ToString();

                if (!string.IsNullOrEmpty(dr["JobDate"].ToString()))
                    drNew["JobDate"] = Convert.ToDateTime(dr["JobDate"].ToString()).ToString("yyyy-MM");
                else
                    drNew["JobDate"] = "-";

                if (!string.IsNullOrEmpty(dr["ConversionDate"].ToString()))
                    drNew["ConversionDate"] = Convert.ToDateTime(dr["ConversionDate"].ToString()).ToString("yyyy-MM");
                else
                    drNew["ConversionDate"] = "-";

                drNew["JobTerm"] = dr["JobTerm"].ToString();
                drNew["FixedJob"] = dr["FixedJob"].ToString();
                drNew["ForeignLanguageName"] = dr["ForeignLanguageName"].ToString();
                drNew["ForeignLanguageResult"] = dr["ForeignLanguageResult"].ToString();
                drNew["PoliticsResult"] = dr["PoliticsResult"].ToString();
                drNew["JobResult"] = dr["JobResult"].ToString();
                drNew["ComputerResult"] = dr["ComputerResult"].ToString();
                drNew["Applyplight"] = dr["Applyplight"].ToString();

                if (!string.IsNullOrEmpty(dr["LastApplyDate"].ToString()))
                    drNew["LastApplyDate"] = Convert.ToDateTime(dr["LastApplyDate"].ToString()).ToString("yyyy-MM");
                else
                    drNew["LastApplyDate"] = "-";

                drNew["SpecialityName"] = dr["SpecialityName"].ToString();
                drNew["SpecialityLevel"] = dr["SpecialityLevel"].ToString();
                drNew["SpecialitapproveUnit"] = dr["SpecialitapproveUnit"].ToString();
                drNew["SpecialitPersonalRank"] = dr["SpecialitPersonalRank"].ToString();

                if (!string.IsNullOrEmpty(dr["SpecialitDateTime"].ToString()))
                    drNew["SpecialitDateTime"] = Convert.ToDateTime(dr["SpecialitDateTime"].ToString()).ToString("yyyy-MM");
                else
                    drNew["SpecialitDateTime"] = "-";

                drNew["AwardsName"] = dr["AwardsName"].ToString();
                drNew["AwardsLevel"] = dr["AwardsLevel"].ToString();
                drNew["AwardsRank"] = dr["AwardsRank"].ToString();
                drNew["AwardsPersonalRank"] = dr["AwardsPersonalRank"].ToString();

                if (!string.IsNullOrEmpty(dr["AwardsDateTime"].ToString()))
                    drNew["AwardsDateTime"] = Convert.ToDateTime(dr["AwardsDateTime"].ToString()).ToString("yyyy-MM");
                else
                    drNew["AwardsDateTime"] = "-";

                if (dr["IDJobRank"].ToString() == "53" && dr["IDSubject"].ToString() == "KY-1")
                {
                    drNew["SpecialityName1"] = dr["SpecialityName1"].ToString();
                    drNew["SpecialityLevel1"] = dr["SpecialityLevel1"].ToString();
                    drNew["SpecialitapproveUnit1"] = dr["SpecialitapproveUnit"].ToString();
                    drNew["SpecialitPersonalRank1"] = dr["SpecialitPersonalRank1"].ToString();

                    if (!string.IsNullOrEmpty(dr["SpecialitDateTime1"].ToString()))
                        drNew["SpecialitDateTime1"] = Convert.ToDateTime(dr["SpecialitDateTime1"].ToString()).ToString("yyyy-MM");
                    else
                        drNew["SpecialitDateTime1"] = "-";

                    drNew["AwardsName1"] = dr["AwardsName1"].ToString();
                    drNew["AwardsLevel1"] = dr["AwardsLevel1"].ToString();
                    drNew["AwardsRank1"] = dr["AwardsRank1"].ToString();
                    drNew["AwardsPersonalRank1"] = dr["AwardsPersonalRank1"].ToString();

                    if (!string.IsNullOrEmpty(dr["AwardsDateTime1"].ToString()))
                        drNew["AwardsDateTime1"] = Convert.ToDateTime(dr["AwardsDateTime1"].ToString()).ToString("yyyy-MM");
                    else
                        drNew["AwardsDateTime1"] = "-";
                }

                drNew["JournalsNo1"] = dr["JournalsNo1"].ToString();
                drNew["ThesisTitle1"] = dr["ThesisTitle1"].ToString();
                drNew["PeriodicalNo1"] = dr["PeriodicalNo1"].ToString();
                drNew["JournalsNo2"] = dr["JournalsNo2"].ToString();
                drNew["ThesisTitle2"] = dr["ThesisTitle2"].ToString();
                drNew["PeriodicalNo2"] = dr["PeriodicalNo2"].ToString();
                drNew["JournalsNo3"] = dr["JournalsNo3"].ToString();
                drNew["ThesisTitle3"] = dr["ThesisTitle3"].ToString();
                drNew["PeriodicalNo3"] = dr["PeriodicalNo3"].ToString();
                //drNew["JournalsNo4"] = dr["JournalsNo4"].ToString();
                //drNew["ThesisTitle4"] = dr["ThesisTitle4"].ToString();
                //drNew["PeriodicalNo4"] = dr["PeriodicalNo4"].ToString();
                drNew["ApplyPrintID"] = dr["ApplyPrintID"].ToString();
                drNew["ApplyStyle"] = dr["ApplyStyle"].ToString();

                drNew["startmonth"] = dr["startmonth"].ToString();
                drNew["startday"] = dr["startday"].ToString();
                drNew["endmonth"] = dr["endmonth"].ToString();
                drNew["endday"] = dr["endday"].ToString();
                drNew["address"] = dr["address"].ToString();
                if(dr["response"].ToString()=="1")
                    drNew["response"] ="有";
                else
                    drNew["response"] = "无";

                strPeriod = dr["ApplyPeriodID"].ToString();
                if (!string.IsNullOrEmpty(dr["ApplyImage"].ToString()))
                    ApplyImage = common.getAppSetting("FolderforLoadPath") + "/" + strPeriod + "/" + dr["ApplyImage"].ToString();//Server.MapPath("UploadFile") + "\\" + ApplyImg;
                else
                    ApplyImage = common.getAppSetting("FolderforLoadPath") + "/blank.JPG";
                drNew["ApplyImage"] = ApplyImage;
                personalXSD.Tables["PersonalInfo"].Rows.Add(drNew);
            }

            RepPrint.DataSource = personalXSD;
            RepPrint.DataBind();
        }

        protected void RepPrint_ItemCreated(object sender, RepeaterItemEventArgs e)
        {
            //if (e.Item.ItemType == ListItemType.Item)
            //{
                
            //    HtmlGenericControl divPagerBreak = (HtmlGenericControl)e.Item.FindControl("divPagerbreak");
            //    if (CurrentCount == intRecordCount)
            //    {
            //        divPagerBreak.Attributes.Remove("class");
            //    }
            //    CurrentCount += 1;
            //}
        }

        protected void RepPrint_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item||e.Item.ItemType== ListItemType.AlternatingItem)
            {

                HtmlGenericControl divPagerBreak = (HtmlGenericControl)e.Item.FindControl("divPagerbreak");
                if (CurrentCount == intRecordCount)
                {
                    divPagerBreak.Attributes.Remove("class");
                }
                CurrentCount += 1;
            }
        }
        
    }
}
